
/**
 * @param {String} url 导出地址
 * @param {String} rsk_token token
 * @param {String} title 标题
 * 导出
*/
export const exportList = (url, rsk_token, title) => {
  // #ifdef H5
  uni.downloadFile({
    url: url,
    header: {
      TOKEN: rsk_token
    },
    success: (res) => {
      const aLink = document.createElement("a");
      aLink.style.display = "none";
      aLink.href = res.tempFilePath;
      aLink.setAttribute("download", title + ".xlsx");
      document.body.appendChild(aLink);
      aLink.click();
      document.body.removeChild(aLink);
      window.URL.revokeObjectURL(url);
    }
  });
  // #endif

  // #ifdef APP-PLUS
  const fullPath = uni.getStorageSync("export_path");
  if (fullPath) {
    uni.removeSavedFile({
      filePath: fullPath,
      complete: function (res) {
        console.log("删除", res);
      }
    });
  }
  uni.downloadFile({
    url: url,
    header: {
      TOKEN: rsk_token
    },
    success: (res) => {
      // 拿到临时文件的绝对路径
      const filepathss = plus.io.convertLocalFileSystemURL(res.tempFilePath);
      // 通过这个路径来拿到他
      plus.io.resolveLocalFileSystemURL(filepathss, function (entry) {
        entry.getParent(function (parentDicEntry) {
          entry.moveTo({ fullPath: parentDicEntry.fullPath + "/" }, title + ".xlsx", function (newFile) {
            uni.setStorageSync("export_path", newFile.fullPath);

            uni.openDocument({
              fileType: "xlsx",
              filePath: newFile.fullPath,
              success: function (res2) {
                console.log("打开文档成功");
              },
              fail: function (err) {
                if (err.errCode === 1 || err.code === 1) {
                  uni.showToast({
                    title: `未找到能打开xlsx类型文件的手机应用`,
                    icon: "none"
                  });
                }
                console.log(err);
              }
            });
          }, function (moveError) {
            uni.showToast({
              title: `已在第三方应用中打开过，请在第三方应用查看时保存`,
              icon: "none"
            });
          });
        });
      });
    }
  });
  // #endif
}
